<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html,body{
            width: 100%;
        }
        .box{
            width: 500px;
            height: 300px;
           
            margin: auto;
        }
    </style>
</head>
<body>
    <div class="box">
        <input type="text" value="1,2,3">输入数字求和，数字之间用半角","号分隔
        <p><button>求和</button> <span id="sum"></span></p>
    </div>
   
</body>
<script>
    var oinput = document.getElementsByTagName('input')[0]
    var obutton = document.getElementsByTagName('button')[0]
    var osum = document.getElementById('sum')

    oinput.onkeyup = function(){
        this.value = this.value.replace(/[^(\d)|(,)]/,'')
    }
    obutton.onclick = function(){
        var num = 0
        var oinput = document.getElementsByTagName('input')[0].value.split(',')
        for(var i in oinput){
            num+=parseInt(oinput[i]) 
        }
        osum.innerHTML = num
    }
    
</script>
</html>